package com.xhs.hawaii.configuration;

import com.alibaba.druid.pool.DruidDataSource;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.io.IOException;
import java.util.Properties;

/**
 * @author tangchuang
 * @version 1.0
 * @since 2018/1/20
 */
@Configuration
@EnableTransactionManagement
@Slf4j
@PropertySource("classpath:profile/${user.env}/jdbc.properties")
@MapperScan(basePackages = {"com.xhs.hawaii.purchase.mapper"})
public class DataSourceConfiguration {

    @Value("${jdbc.driver-class-name}")
    private String driverClassName;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;

    @Bean(destroyMethod = "close")
    public DruidDataSource createDataSource() throws IOException {
        log.info("====== DataSource init start ! =======");
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        Properties prop = new Properties();
        prop.load(this.getClass().getResourceAsStream("/druid.properties"));
        log.info("====== DataSource init success ! =======");
        return dataSource;
    }
}
